package com.navcoo.yidaaddproapi.service.serviceImpl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.navcoo.yidaaddproapi.mapper.DataSourceMapper;
import com.navcoo.yidaaddproapi.pojo.dao.DataSourceDAO;
import com.navcoo.yidaaddproapi.response.Result;
import com.navcoo.yidaaddproapi.service.DataSourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据源服务Impl
 *
 * @author Navcoo
 * @date 2024-04-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DataSourceServiceImpl implements DataSourceService {

    private final DataSourceMapper dataSourceMapper;
    /**
     * 获取数据源数据
     *
     * @return {@link Result}<{@link JSONObject}>
     */
    @Override
    public Result<List<JSONObject>> getDataSourceDatas() {
        log.info("获取数据源数据");
        List<DataSourceDAO> dataSourceDatas = dataSourceMapper.selectList(null);
        log.info("获取数据源数据成功,dataSourceDatas:{}",dataSourceDatas);

        List<JSONObject> result = new ArrayList<>();
        dataSourceDatas.stream().collect(Collectors.groupingBy(DataSourceDAO::getClassName)).forEach((k,v)->{
            result.add(JSONUtil.createObj()
                    .set("className",k)
                    .set("content",v.stream().map(d -> JSONUtil.createObj()
                            .set("apiName",d.getApiName())
                            .set("interfacePath",d.getInterfacePath())
                            .set("requestType",d.getRequestType())
                            .set("params",JSONUtil.parse(d.getParams()))
                    ).toList()));
        });
        log.info("dataSourceDatas:{}", JSONUtil.toJsonStr(dataSourceDatas));

        return Result.success(result);
    }


    /**
     * 插入数据源数据
     */
    @Override
    public void insertDataSourceDatas() {

        String a = "[{\"className\":\"表单相关 API\",\"apiName\":\"新建表单实例\",\"interfacePath\":\"/v1/form/saveFormData.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"appType\\\",\\\"description\\\":\\\"应用 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"formDataJson\\\",\\\"description\\\":\\\"表单数据\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"更新表单中指定组件值\",\"interfacePath\":\"/v1/form/updateFormData.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"formInstId\\\",\\\"description\\\":\\\"要更新的表单数据 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"updateFormDataJson\\\",\\\"description\\\":\\\"要更新的表单组件值，必填\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"useLatestVersion\\\",\\\"description\\\":\\\"使用最新的表单版本进行更新\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"删除表单实例\",\"interfacePath\":\"/v1/form/deleteFormData.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"formInstId\\\",\\\"description\\\":\\\"要删除的表单数据 ID\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"根据表单实例 ID 查询表单实例详情\",\"interfacePath\":\"/v1/form/getFormDataById.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formInstId\\\",\\\"description\\\":\\\"要查询的表单数据 ID\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"根据条件搜索表单实例 ID 列表\",\"interfacePath\":\"/v1/form/searchFormDataIds.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"searchFieldJson\\\",\\\"description\\\":\\\"根据表单内组件值查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"originatorId\\\",\\\"description\\\":\\\"根据数据提交人工号查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createFrom\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createTo\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedFrom\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedTo\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"dynamicOrder\\\",\\\"description\\\":\\\"排序\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"根据条件搜索表单实例详情列表\",\"interfacePath\":\"/v1/form/searchFormDatas.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"searchFieldJson\\\",\\\"description\\\":\\\"根据表单内组件值查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"originatorId\\\",\\\"description\\\":\\\"根据数据提交人工号查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createFrom\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createTo\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedFrom\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedTo\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"dynamicOrder\\\",\\\"description\\\":\\\"排序\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"获取表单定义\",\"interfacePath\":\"/v1/form/getFormComponentDefinationList.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"version\\\",\\\"description\\\":\\\"表单版本\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"表单相关 API\",\"apiName\":\"获取子表单数据\",\"interfacePath\":\"/v1/form/listTableDataByFormInstIdAndTableId.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"formInstanceId\\\",\\\"description\\\":\\\"要查询的实例的实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"tableFieldId\\\",\\\"description\\\":\\\"需要查找的子表单组件的唯一标识\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"流程发起\",\"interfacePath\":\"/v1/process/startInstance.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"processCode\\\",\\\"description\\\":\\\"流程 code\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"formDataJson\\\",\\\"description\\\":\\\"表单数据\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"deptId\\\",\\\"description\\\":\\\"发起人所在部门号\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"根据条件搜索流程实例 ID\",\"interfacePath\":\"/v1/process/getInstanceIds.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"searchFieldJson\\\",\\\"description\\\":\\\"根据表单内组件值查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"taskId\\\",\\\"description\\\":\\\"\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"instanceStatxus\\\",\\\"description\\\":\\\"实例状态\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"approvedResult\\\",\\\"description\\\":\\\"流程审批结果\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"originatorId\\\",\\\"description\\\":\\\"根据流程发起人工号查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createFrom\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createTo\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedFrom\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedTo\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"根据搜索条件获取实例详情列表\",\"interfacePath\":\"/v1/process/getInstances.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"formUuid\\\",\\\"description\\\":\\\"表单 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"searchFieldJson\\\",\\\"description\\\":\\\"根据表单内组件值查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"taskId\\\",\\\"description\\\":\\\"任务 ID\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"instanceStatus\\\",\\\"description\\\":\\\"实例状态\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"approvedResult\\\",\\\"description\\\":\\\"流程审批结果\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"originatorId\\\",\\\"description\\\":\\\"根据数据提交人工号查询\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createFrom\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"createTo\\\",\\\"description\\\":\\\"createFrom 和 createTo 两个时间构造一个时间段。查询在该时间段创建的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedFrom\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"modifiedTo\\\",\\\"description\\\":\\\"modifiedFrom 和 modifiedTo 构成一个时间段，查询在该时间段有修改的数据列表\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"根据实例 ID 获取流程实例详情\",\"interfacePath\":\"/v1/process/getInstanceById.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"processInstanceId\\\",\\\"description\\\":\\\"流程实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"删除流程实例\",\"interfacePath\":\"/v1/process/deleteInstance.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"processInstanceId\\\",\\\"description\\\":\\\"流程实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"终止流程实例\",\"interfacePath\":\"/v1/process/terminateInstance.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"processInstanceId\\\",\\\"description\\\":\\\"流程实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"执行单个任务接口\",\"interfacePath\":\"/v1/task/executeTask.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"taskId\\\",\\\"description\\\":\\\"任务 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"procInstId\\\",\\\"description\\\":\\\"实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"outResult\\\",\\\"description\\\":\\\"审批结果\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"remark\\\",\\\"description\\\":\\\"审批意见\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"formDataJson\\\",\\\"description\\\":\\\"表单数据\\\",\\\"requiredFields\\\":\\\"否\\\"},{\\\"parameterName\\\":\\\"noExecuteExpressions\\\",\\\"description\\\":\\\"是否不执行校验&关联操作\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"获取审批记录\",\"interfacePath\":\"/v1/process/getOperationRecords.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"processInstanceId\\\",\\\"description\\\":\\\"流程实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"流程相关 API\",\"apiName\":\"流程实例更新\",\"interfacePath\":\"/v1/process/updateInstance.json\",\"requestType\":\"POST\",\"params\":\"[{\\\"parameterName\\\":\\\"processInstanceId\\\",\\\"description\\\":\\\"实例 ID\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"updateFormDataJson\\\",\\\"description\\\":\\\"更新的表单数据\\\",\\\"requiredFields\\\":\\\"是\\\"}]\"},{\"className\":\"任务中心相关 API\",\"apiName\":\"已提交任务\",\"interfacePath\":\"/v1/process/getMySubmitInApp.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"keyword\\\",\\\"description\\\":\\\"关键词\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"任务中心相关 API\",\"apiName\":\"待办任务\",\"interfacePath\":\"/v1/task/getTodoTasksInApp.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"keyword\\\",\\\"description\\\":\\\"关键词\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"任务中心相关 API\",\"apiName\":\"已完成任务\",\"interfacePath\":\"/v1/task/getDoneTasksInApp.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"keyword\\\",\\\"description\\\":\\\"关键词\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"},{\"className\":\"任务中心相关 API\",\"apiName\":\"抄送我的任务（应用纬度）\",\"interfacePath\":\"/v1/task/getNotifyMeTasksInApp.json\",\"requestType\":\"GET\",\"params\":\"[{\\\"parameterName\\\":\\\"pageSize\\\",\\\"description\\\":\\\"每页记录数\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"currentPage\\\",\\\"description\\\":\\\"当前页\\\",\\\"requiredFields\\\":\\\"是\\\"},{\\\"parameterName\\\":\\\"keyword\\\",\\\"description\\\":\\\"关键词\\\",\\\"requiredFields\\\":\\\"否\\\"}]\"}]";
        JSONArray aArr = JSONUtil.parseArray(a);

        log.info("aArr:{}",aArr);

        for (Object aJson : aArr){
            log.info("aJson:{}",aJson);

            DataSourceDAO dataSourceDAO = DataSourceDAO.builder()
                    .className(((JSONObject) aJson).getStr("className"))
                    .apiName(((JSONObject) aJson).getStr("apiName"))
                    .interfacePath(((JSONObject) aJson).getStr("interfacePath"))
                    .requestType(((JSONObject) aJson).getStr("requestType"))
                    .params(((JSONObject) aJson).getStr("params"))
                    .build();
            int insert = dataSourceMapper.insert(dataSourceDAO);

            log.info("insert:{}",insert);

        }

    }

}

